package cn.com.cetccst.appstore.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import cn.com.cetccst.appstore.BR;
import cn.com.cetccst.appstore.R;
import cn.com.cetccst.appstore.database.model.CategoryModel;
import cn.com.cetccst.appstore.databinding.FragmentCategoryListBinding;
import cn.com.cetccst.appstore.ui.activity.CategoryAppListActivity;
import cn.com.cetccst.appstore.ui.fragment.viewmodel.CategoryListViewModel;
import cn.com.cetccst.uicomponents.binding.app.DataBindingConfig;

public class CategoryListFragment extends BaseFragment<FragmentCategoryListBinding> {

    private CategoryListViewModel viewModel;

    private CategoryListAdapter adapter;

    @Override
    protected void initViewModel() {
        viewModel = getFragmentScopeViewModel(CategoryListViewModel.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        adapter = new CategoryListAdapter(getContext(), item -> {
            Intent intent = new Intent(getContext(), CategoryAppListActivity.class);
            intent.putExtra(CategoryAppListActivity.EXTRA_CATEGORY_ID, item.getId());
            intent.putExtra(CategoryAppListActivity.EXTRA_CATEGORY_NAME, item.getName());
            startActivity(intent);
        });

        return new DataBindingConfig(R.layout.fragment_category_list, BR.viewModel, viewModel)
                .addBindingParam(BR.adapter, adapter)
                .addBindingParam(BR.refreshListener, (SwipeRefreshLayout.OnRefreshListener) () -> viewModel.requestCategoryList(getContext()));
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewModel.requestCategoryList(getContext());
    }

    @Override
    public void search(String key) {
        super.search(key);
        viewModel.requestCategoryList(getContext());
    }

    public interface OnFragmentInteractionListener {
        void onListFragmentInteraction(CategoryModel item);
    }

}
